package org.martin.snsrs.client;

import java.io.File;

import org.martin.snsrs.client.services.http.SnsrsApi;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class EditNewActivity extends MyActivityBase implements OnClickListener {

	private Button back;

	private Button publish;

	private EditText activityName;

	private EditText startTime;

	private EditText endTime;

	private EditText activityLocation;

	private EditText activityDesc;

	private ImageView activityPic;

	private SnsrsApi api;

	private String imageSource = "/mnt/sdcard/1.jpg";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_new);
		init();
	}

	private void init() {
		api = getSharedApplication().getSnsrsApi();
		back = (Button) findViewById(R.id.btn_cancel_send);
		back.setOnClickListener(this);
		publish = (Button) findViewById(R.id.btn_send);
		publish.setOnClickListener(this);

		activityName = (EditText) findViewById(R.id.activity_name);
		startTime = (EditText) findViewById(R.id.start_time);
		endTime = (EditText) findViewById(R.id.end_time);
		activityLocation = (EditText) findViewById(R.id.activity_location);
		activityDesc = (EditText) findViewById(R.id.activity_desc);
		activityPic = (ImageView) findViewById(R.id.activity_pic);
		startTime.setText("2012-12-13 12:15:12");
		endTime.setText("2012-12-13 12:15:12");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_cancel_send:
			exitPublish();
			break;
		case R.id.btn_send:
			publish();
			break;
		}
	}

	private void exitPublish() {
		changeActivity(new Intent(this, MainActivity.class));
	}

	private void publish() {
		AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

			@Override
			protected Void doInBackground(Void... params) {
				try {
					api.publishNewActivity(activityName.getText().toString(),
							startTime.getText().toString(), endTime.getText()
									.toString(), activityLocation.getText()
									.toString(), activityDesc.getText()
									.toString(), new File(imageSource));
					api.login("a", "1");
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}

		};
		task.execute(new Void[0]);
	}

}
